package com.zhihuichongcheng.application.base;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.smartlib.cmnObject.util.LogUtil;
import com.smartlib.cmnObject.util.SPUtil;
import com.umeng.analytics.MobclickAgent;
import com.zhihuichongcheng.application.R;
import com.zhihuichongcheng.application.bean.mine.UserBean;
import com.zhihuichongcheng.application.net.HttpManager;
import com.zhihuichongcheng.application.ui.BaseApplication;
import com.zhihuichongcheng.application.util.CollectionUtils;
import com.zhihuichongcheng.application.util.StatusBarUtil;
import com.zhihuichongcheng.application.util.SystemBarTintManager;

import java.util.LinkedHashSet;
import java.util.Set;

public class BaseActivity extends FragmentActivity {
    public static final String EXTRA_TITLE = "extra_title";
    public static final int EXTRA_RESPONSE_CODE_SUCCESS = 200;
    protected UserBean mUser;
    protected boolean isStartAnimation = true;
    protected BaseApplication app;
    protected Context mContext;
    protected HttpManager mManager;
    protected SPUtil spUtil = null;
    protected String title = "";
    protected Set<OnActivityGetResult> onActivityGetResults = new LinkedHashSet<>();
    private ImageView mImBack, mImRight;
    private TextView mTvTitle, mTvArrowTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LogUtil.logD(getClass().getSimpleName() + "\tonCreate");
        if (null != getIntent().getExtras()) {
            if (getIntent().getExtras().containsKey(EXTRA_TITLE)) {
                title = getIntent().getExtras().getString(EXTRA_TITLE);
            }
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //透明状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //透明导航栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        app = (BaseApplication) getApplication();
        mUser = app.getUser();
        spUtil = SPUtil.getInstance(this);
        mContext = this;
        mManager = HttpManager.getInstance(mContext);
        app.addActivity(this);
    }


    public void setSystemBarColor(int color) {
        SystemBarTintManager tintManager = new SystemBarTintManager(this);
        tintManager.setStatusBarTintEnabled(true);
        tintManager.setStatusBarTintResource(color);
    }

    @Override
    protected void onStart() {
        super.onStart();
        setTitle(title);
    }

    public void setTitle(String title) {
        TextView titleView = (TextView) findViewById(R.id.navbar_title);
        this.title = title;
        if (null != titleView)
            titleView.setText(title);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
        MobclickAgent.onPageEnd("base");
    }

    @Override
    protected void onResume() {
        super.onResume();
        LinearLayout backBtn = (LinearLayout) findViewById(R.id.navbar_back);
        if (backBtn != null) {
            backBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onFinished();
                    finish();
                }
            });
        }
        MobclickAgent.onResume(this);
        MobclickAgent.onPageEnd("base");
    }

    protected void onFinished() {
    }

    @Override
    public void finish() {
        super.finish();
        app.removeActivity(this);
        if (isStartAnimation) {
            overridePendingTransition(R.anim.activity_transle_enter, R.anim.activity_transle_out);
        }
    }

    public void onBaClick(final View.OnClickListener onClickListener) {
        View backBtn = findViewById(R.id.navbar_back);
        if (backBtn != null) {
            backBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    onClickListener.onClick(view);
                }
            });
        }
    }

    public void setRightTvUnClick(String title) {
        View navbar = this.findViewById(R.id.navbar);
        TextView tvRight = (TextView) navbar.findViewById(R.id.tv_right);
        tvRight.setVisibility(View.VISIBLE);
        tvRight.setText(title);
        tvRight.setClickable(false);
        tvRight.setAlpha(0.3f);
    }

    public void setArrowTitle(String title) {
        getTvArrowTitle().setText(title);
    }

    public synchronized TextView getTvArrowTitle() {
        if (null == mTvArrowTitle) {
            mTvArrowTitle = (TextView) findViewById(R.id.tv_arrow_title);
        }
        return mTvArrowTitle;
    }

    public void setTitleColor(int color) {
        View navbar = this.findViewById(R.id.navbar);
        TextView titleView = (TextView) navbar.findViewById(R.id.navbar_title);
        titleView.setTextColor(getResources().getColor(color));
    }

    protected void setImRight(int resId, View.OnClickListener listener) {
        View navbar = this.findViewById(R.id.navbar);
        if (null == navbar) {
            navbar = this.findViewById(R.id.title);
        }
        if (null == navbar) {
            return;
        }
        ImageView imRight = (ImageView) navbar.findViewById(R.id.im_right);
        imRight.setVisibility(View.VISIBLE);
        imRight.setImageResource(resId);
        if (listener != null) {
            imRight.setOnClickListener(listener);
        }

    }

    protected void setTvRight(String title, View.OnClickListener listener) {
        View navbar = this.findViewById(R.id.navbar);
        TextView tvRight = (TextView) navbar.findViewById(R.id.tv_right);
        tvRight.setVisibility(View.VISIBLE);
        tvRight.setClickable(true);
        tvRight.setAlpha(1f);
        tvRight.setText(title);
        if (listener != null) {
            tvRight.setOnClickListener(listener);
        }
    }

    protected void setTvRight2(String title, View.OnClickListener listener) {
        View navbar = this.findViewById(R.id.navbar);
        TextView tvRight = (TextView) navbar.findViewById(R.id.tv_right_2);
        tvRight.setVisibility(View.VISIBLE);
        tvRight.setClickable(true);
        tvRight.setAlpha(1f);
        tvRight.setText(title);
        if (listener != null) {
            tvRight.setOnClickListener(listener);
        }
    }

    public void setTitle(String title, int color) {
        TextView titleView = (TextView) findViewById(R.id.navbar_title);
        titleView.setTextColor(color);
        titleView.setText(title);
    }

    public void flatStatusBar() {
        StatusBarUtil.transparencyBar(this);
    }

    public void setStatusBarLightMode() {
        StatusBarUtil.StatusBarLightMode(this);
    }

    public void setStatusBarDarkMode() {
        StatusBarUtil.StatusBarDarkMode(this);
    }

    protected void startActivity(Class cls) {
        Intent intent = new Intent(mContext, cls);
        startActivity(intent);
        overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
    }

    protected void startActivityWithIntent(Intent intent) {
        startActivity(intent);
        overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
    }

    protected void startActivity(Class cls, Bundle bundle) {
        Intent intent = new Intent(mContext, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
        overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
    }

    protected void startActivity(String action) {
        Intent intent = new Intent(action);
        startActivity(intent);
        overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
    }

    public void startActivityForResult(Class cls, int RequestCode, Bundle bundle) {
        Intent intent = new Intent(mContext, cls);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivityForResult(intent, RequestCode);
        overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
    }

    protected void hideKeyboard(EditText edit) {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(edit.getWindowToken(), 0);
    }

    protected void showKeyboard(EditText edit) {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
    }

    public Context getContext() {
        return mContext;
    }

    @Override
    protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        CollectionUtils.iterator(onActivityGetResults, new CollectionUtils.AIIteratorListCallBack() {
            @Override
            public boolean callback(Object obj, int position) {
                ((OnActivityGetResult) obj).onActivityResult(requestCode, resultCode, data);
                return false;
            }
        });
    }

    public void addOnGetResult(OnActivityGetResult onActivityGetResult) {
        if (null != onActivityGetResult)
            this.onActivityGetResults.add(onActivityGetResult);
    }

    public interface OnActivityGetResult {
        void onActivityResult(int requestCode, int resultCode, Intent data);
    }
}
